<template>
	<view class="container">
		<!-- 订单状态卡片 -->
		<view class="status-card">
			<view class="status-icon" :class="'icon-' + order.status">
				<text class="iconfont" :class="getStatusIcon(order.status)"></text>
			</view>
			<view class="status-info">
				<view class="status-text">{{ getStatusText(order.status) }}</view>
				<view class="status-desc">{{ getStatusDesc(order.status) }}</view>
			</view>
		</view>
		
		<!-- 物流信息 -->
		<view class="delivery-info" v-if="order.status !== 'unpaid' && order.status !== 'cancelled'">
			<view class="section-title">
				<text class="title">物流信息</text>
				<text class="more" v-if="order.status !== 'unshipped'" @click="viewDelivery">查看物流 ></text>
			</view>
			<view class="delivery-content" v-if="order.status !== 'unshipped'">
				<view class="delivery-company">{{ order.deliveryCompany }}</view>
				<view class="delivery-no">物流单号：{{ order.deliveryNo }}</view>
				<view class="delivery-status">{{ order.latestDelivery }}</view>
			</view>
			<view class="delivery-content waiting" v-else>
				<text class="waiting-text">等待商家发货</text>
			</view>
		</view>
		
		<!-- 收货地址 -->
		<view class="address-info">
			<view class="section-title">
				<text class="title">收货信息</text>
			</view>
			<view class="address-content">
				<view class="address-name-phone">
					<text class="name">{{ order.address.name }}</text>
					<text class="phone">{{ order.address.phone }}</text>
				</view>
				<view class="address-detail">{{ getFullAddress(order.address) }}</view>
			</view>
		</view>
		
		<!-- 订单信息 -->
		<view class="order-info">
			<view class="section-title">
				<text class="title">订单信息</text>
			</view>
			<view class="info-item">
				<text class="label">订单编号</text>
				<text class="value">{{ order.orderNo }}</text>
				<text class="copy" @click="copyOrderNo">复制</text>
			</view>
			<view class="info-item">
				<text class="label">下单时间</text>
				<text class="value">{{ order.createTime }}</text>
			</view>
			<view class="info-item" v-if="order.payTime">
				<text class="label">支付时间</text>
				<text class="value">{{ order.payTime }}</text>
			</view>
			<view class="info-item" v-if="order.deliveryTime">
				<text class="label">发货时间</text>
				<text class="value">{{ order.deliveryTime }}</text>
			</view>
			<view class="info-item" v-if="order.completeTime">
				<text class="label">完成时间</text>
				<text class="value">{{ order.completeTime }}</text>
			</view>
			<view class="info-item">
				<text class="label">支付方式</text>
				<text class="value">{{ order.paymentMethod || '未支付' }}</text>
			</view>
			<view class="info-item" v-if="order.remark">
				<text class="label">备注信息</text>
				<text class="value">{{ order.remark }}</text>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="section-title">
				<text class="title">商品信息</text>
			</view>
			<view class="goods-item" v-for="(goods, index) in order.goods" :key="index">
				<image class="goods-image" :src="goods.image" mode="aspectFill"></image>
				<view class="goods-info">
					<view class="goods-name">{{ goods.name }}</view>
					<view class="goods-spec">{{ goods.spec }}</view>
					<view class="goods-price-qty">
						<text class="goods-price">¥{{ goods.price }}</text>
						<text class="goods-qty">x{{ goods.quantity }}</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 价格信息 -->
		<view class="price-info">
			<view class="price-item">
				<text class="label">商品金额</text>
				<text class="value">¥{{ order.productAmount }}</text>
			</view>
			<view class="price-item">
				<text class="label">优惠金额</text>
				<text class="value">-¥{{ order.discountAmount }}</text>
			</view>
			<view class="price-item">
				<text class="label">运费</text>
				<text class="value">+¥{{ order.deliveryFee }}</text>
			</view>
			<view class="price-item total">
				<text class="label">实付款</text>
				<text class="value">¥{{ order.totalAmount }}</text>
			</view>
		</view>
		
		<!-- 底部操作区域 -->
		<view class="footer-action">
			<view class="action-btns">
				<view class="btn outline" v-if="order.status === 'unpaid'" @click="handleCancel">取消订单</view>
				<view class="btn primary" v-if="order.status === 'unpaid'" @click="handlePay">去支付</view>
				<view class="btn primary" v-if="order.status === 'shipped'" @click="handleConfirm">确认收货</view>
				<view class="btn outline" v-if="order.status === 'unshipped' || order.status === 'shipped'" @click="handleContact">联系商家</view>
				<view class="btn primary" v-if="order.status === 'received'" @click="handleComment">去评价</view>
				<view class="btn outline" v-if="order.status === 'completed' || order.status === 'cancelled'" @click="handleDelete">删除订单</view>
				<view class="btn outline" v-if="order.status === 'completed'" @click="handleBuyAgain">再次购买</view>
			</view>
		</view>
	</view>
</template>

<script>
import { orderApi } from '@/api/index';

export default {
	data() {
		return {
			orderId: 0,
			order: {
				id: 0,
				orderNo: '',
				status: 'unpaid',
				goods: [],
				address: {
					name: '',
					phone: '',
					province: '',
					city: '',
					district: '',
					detail: ''
				},
				deliveryCompany: '',
				deliveryNo: '',
				latestDelivery: '',
				createTime: '',
				payTime: '',
				deliveryTime: '',
				completeTime: '',
				paymentMethod: '',
				remark: '',
				productAmount: 0,
				discountAmount: 0,
				deliveryFee: 0,
				totalAmount: 0
			}
		}
	},
	onLoad(options) {
		if (options.id) {
			this.orderId = options.id;
			this.getOrderDetail();
		}
	},
	methods: {
		// 获取订单详情
		async getOrderDetail() {
			try {
				uni.showLoading({
					title: '加载中...'
				});
				
				const res = await orderApi.getOrderDetail(this.orderId);
				this.order = res.data;
				
			} catch (e) {
				console.error('获取订单详情失败', e);
				uni.showToast({
					title: '获取订单详情失败',
					icon: 'none'
				});
			} finally {
				uni.hideLoading();
			}
		},
		
		// 获取状态文本
		getStatusText(status) {
			const statusMap = {
				'unpaid': '待付款',
				'unshipped': '待发货',
				'shipped': '待收货',
				'received': '待评价',
				'completed': '已完成',
				'cancelled': '已取消'
			};
			return statusMap[status] || '未知状态';
		},
		
		// 获取状态图标
		getStatusIcon(status) {
			const iconMap = {
				'unpaid': 'icon-wallet',
				'unshipped': 'icon-box',
				'shipped': 'icon-truck',
				'received': 'icon-comment',
				'completed': 'icon-check',
				'cancelled': 'icon-close'
			};
			return iconMap[status] || 'icon-question';
		},
		
		// 获取状态描述
		getStatusDesc(status) {
			const descMap = {
				'unpaid': '请在24小时内完成支付，超时订单将自动取消',
				'unshipped': '商家正在处理您的订单，请耐心等待',
				'shipped': '商品已经发出，请注意查收',
				'received': '您已收到商品，请尽快评价',
				'completed': '订单已完成，感谢您的购买',
				'cancelled': '订单已取消'
			};
			return descMap[status] || '';
		},
		
		// 获取完整地址
		getFullAddress(address) {
			return `${address.province} ${address.city} ${address.district} ${address.detail}`;
		},
		
		// 复制订单号
		copyOrderNo() {
			uni.setClipboardData({
				data: this.order.orderNo,
				success: () => {
					uni.showToast({
						title: '复制成功'
					});
				}
			});
		},
		
		// 查看物流
		viewDelivery() {
			uni.navigateTo({
				url: `/pages/order/delivery?id=${this.orderId}`
			});
		},
		
		// 支付订单
		handlePay() {
			uni.navigateTo({
				url: `/pages/order/pay?id=${this.orderId}`
			});
		},
		
		// 取消订单
		handleCancel() {
			uni.showModal({
				title: '取消订单',
				content: '确认要取消该订单吗？',
				success: async (res) => {
					if (res.confirm) {
						try {
							await orderApi.cancelOrder(this.orderId);
							uni.showToast({
								title: '取消订单成功'
							});
							// 刷新订单详情
							this.getOrderDetail();
						} catch (e) {
							uni.showToast({
								title: '取消订单失败',
								icon: 'none'
							});
						}
					}
				}
			});
		},
		
		// 确认收货
		handleConfirm() {
			uni.showModal({
				title: '确认收货',
				content: '确认已收到该订单商品吗？',
				success: async (res) => {
					if (res.confirm) {
						try {
							await orderApi.confirmOrder(this.orderId);
							uni.showToast({
								title: '确认收货成功'
							});
							// 刷新订单详情
							this.getOrderDetail();
						} catch (e) {
							uni.showToast({
								title: '确认收货失败',
								icon: 'none'
							});
						}
					}
				}
			});
		},
		
		// 联系商家
		handleContact() {
			uni.makePhoneCall({
				phoneNumber: '400-123-4567'
			});
		},
		
		// 评价
		handleComment() {
			uni.navigateTo({
				url: `/pages/comment/create?orderId=${this.orderId}`
			});
		},
		
		// 删除订单
		handleDelete() {
			uni.showModal({
				title: '删除订单',
				content: '确认要删除该订单吗？删除后将不可恢复',
				success: async (res) => {
					if (res.confirm) {
						try {
							await orderApi.deleteOrder(this.orderId);
							uni.showToast({
								title: '删除成功'
							});
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);
						} catch (e) {
							uni.showToast({
								title: '删除失败',
								icon: 'none'
							});
						}
					}
				}
			});
		},
		
		// 再次购买
		handleBuyAgain() {
			// 获取订单的第一个商品进行购买
			if (this.order.goods && this.order.goods.length > 0) {
				const firstGoods = this.order.goods[0];
				uni.navigateTo({
					url: `/pages/product/detail?id=${firstGoods.productId}`
				});
			}
		}
	}
}
</script>

<style lang="scss">
.container {
	background-color: #f8f8f8;
	min-height: 100vh;
	padding-bottom: 120rpx;
}

.section-title {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 20rpx 0;
	
	.title {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
	}
	
	.more {
		font-size: 24rpx;
		color: #666;
	}
}

.status-card {
	background-color: #FF5151;
	padding: 40rpx 30rpx;
	display: flex;
	align-items: center;
	color: #fff;
	
	.status-icon {
		width: 80rpx;
		height: 80rpx;
		background-color: rgba(255, 255, 255, 0.2);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 20rpx;
		
		.iconfont {
			font-size: 40rpx;
		}
	}
	
	.status-info {
		flex: 1;
		
		.status-text {
			font-size: 34rpx;
			font-weight: bold;
			margin-bottom: 10rpx;
		}
		
		.status-desc {
			font-size: 24rpx;
			opacity: 0.8;
		}
	}
}

.delivery-info,
.address-info,
.order-info,
.goods-list,
.price-info {
	background-color: #fff;
	margin: 20rpx;
	padding: 20rpx;
	border-radius: 12rpx;
}

.delivery-content {
	padding: 20rpx 0;
	
	.delivery-company {
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}
	
	.delivery-no {
		font-size: 24rpx;
		color: #666;
		margin-bottom: 10rpx;
	}
	
	.delivery-status {
		font-size: 24rpx;
		color: #999;
	}
	
	&.waiting {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100rpx;
		
		.waiting-text {
			font-size: 28rpx;
			color: #999;
		}
	}
}

.address-content {
	padding: 20rpx 0;
	
	.address-name-phone {
		margin-bottom: 10rpx;
		
		.name {
			font-size: 28rpx;
			font-weight: bold;
			margin-right: 20rpx;
		}
		
		.phone {
			font-size: 28rpx;
			color: #666;
		}
	}
	
	.address-detail {
		font-size: 28rpx;
		color: #333;
		line-height: 1.5;
	}
}

.info-item {
	display: flex;
	padding: 16rpx 0;
	font-size: 28rpx;
	
	.label {
		width: 150rpx;
		color: #999;
	}
	
	.value {
		flex: 1;
		color: #333;
	}
	
	.copy {
		color: #FF5151;
		padding: 0 10rpx;
	}
}

.goods-item {
	display: flex;
	padding: 20rpx 0;
	border-bottom: 1rpx solid #f5f5f5;
	
	&:last-child {
		border-bottom: none;
	}
	
	.goods-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
		background-color: #f5f5f5;
	}
	
	.goods-info {
		flex: 1;
		position: relative;
		
		.goods-name {
			font-size: 28rpx;
			color: #333;
			margin-bottom: 10rpx;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
		}
		
		.goods-spec {
			font-size: 24rpx;
			color: #999;
			margin-bottom: 10rpx;
		}
		
		.goods-price-qty {
			display: flex;
			justify-content: space-between;
			
			.goods-price {
				font-size: 28rpx;
				color: #FF5151;
			}
			
			.goods-qty {
				font-size: 24rpx;
				color: #999;
			}
		}
	}
}

.price-info {
	.price-item {
		display: flex;
		justify-content: space-between;
		padding: 10rpx 0;
		font-size: 28rpx;
		
		.label {
			color: #666;
		}
		
		.value {
			color: #333;
		}
		
		&.total {
			margin-top: 10rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid #f5f5f5;
			
			.label {
				font-weight: bold;
				color: #333;
			}
			
			.value {
				color: #FF5151;
				font-weight: bold;
				font-size: 32rpx;
			}
		}
	}
}

.footer-action {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: #fff;
	padding: 20rpx;
	display: flex;
	justify-content: flex-end;
	box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
	
	.action-btns {
		display: flex;
		
		.btn {
			min-width: 160rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			border-radius: 35rpx;
			font-size: 28rpx;
			margin-left: 20rpx;
			
			&.primary {
				background-color: #FF5151;
				color: #fff;
			}
			
			&.outline {
				border: 1rpx solid #ddd;
				color: #666;
			}
		}
	}
}
</style> 